'use strict';

const Controller = require('egg').Controller;

class LineController extends Controller {

    /**
     * 获取线路列表
     */
    async getLineList(){
        const { ctx } = this;
        const serviceId = ctx.session.serviceId;
        // 线路列表参数
        ctx.state.httpOption = {
            url: ['public', 'route','list'],
            data: {
                serviceId ,
                currPage: 1,
                pageSize: 100
            }
        }
        // 线路列表数据
        await this.service.handleRequest.requestDate();
        if (ctx.status === 200)
            ctx.body = ctx.state.result.data;
    }

    /**
     * 获取线路详情
     */
    async getLinePoints() {
        const { ctx } = this;
        const id = Number(ctx.params.id);
        ctx.session.lineData = {};

        // 线路详情参数
        ctx.state.httpOption = {
            url: ['public', 'route', 'routePoin'],
            data: {
                id:id
            }
        }
        // 获取线路数据
        await this.service.handleRequest.requestDate();
        if (ctx.status === 200) {       
            // ctx.session.lineData = JSON.stringify(ctx.state.result.data);
            ctx.body = ctx.state.result.data // 缓存当前线路数据    
        } else {
            ctx.body = {
                status: 402,
                message: 'miss data'
            }
        }
            
    }

    async showLineInfo() {
        const { ctx } = this;
        const id = ctx.params.id;
        let lineData = {};

        // 线路详情参数
        ctx.state.httpOption = {
            url: ['public', 'route', 'routeInfo'],
            data: {
                id: id
            }
        }
        // 获取线路数据
        await this.service.handleRequest.requestDate();

        if (ctx.status === 200) lineData = ctx.state.result.data;
    
        await ctx.render('line/detail',{
            title: '公共服务',
            lineData
        })
    }

}
module.exports = LineController;